//
//  LeftSortsViewController.m
//  LGDeckViewController
//
//  Created by jamie on 15/3/31.
//  Copyright (c) 2015年 Jamie-Ling. All rights reserved.


#import "LeftSortsViewController.h"
#import "AppDelegate.h"
#import "otherViewController.h"
#import "myNewsViewController.h"
#import "MyDynamicViewController.h"
#import "myCollectViewController.h"
#import "myTagViewController.h"
#import "YXSetttingController.h"
#import "personalDataViewController.h"
#import "MyDynamicViewController.h"
#import "myCollectViewController.h"
#import "myTagViewController.h"
#import "loginView.h"
#import "UIAlertController+Category.h"




@interface LeftSortsViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UILabel *nikeLabel;

@property (nonatomic, strong) UIButton *icon;

@property (nonatomic, strong) UILabel *sexLab;

@property (nonatomic, strong) loginView *loginV;




@end

@implementation LeftSortsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageview.image = [UIImage imageNamed:@"leftbackiamge"];
    
    [self.view addSubview:imageview];

    UITableView *tableview = [[UITableView alloc] init];
    self.tableview = tableview;
    tableview.dataSource = self;
    tableview.delegate  = self;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    

     tableview.frame = self.view.bounds;    
    [self.view addSubview:tableview];

    //监听者模式
    [[UserModel sharedUserModel] addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}

//将要出现
-(void)viewDidAppear:(BOOL)animated
{    
    [self aa];
}


- (void)aa{
    if ([UserModel sharedUserModel].isLogin) {
        self.nikeLabel.text = [UserModel sharedUserModel].name;
        UIImage * image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[UserModel sharedUserModel].headImage]]];
        [self.icon setImage:image forState:UIControlStateNormal];
        
        self.sexLab.text = [UserModel sharedUserModel].sex ;
    }else{
        self.nikeLabel.text = @"未登录";
        [self.icon setImage:[UIImage imageNamed:@" NoLoginHead"] forState:UIControlStateNormal];
//        self.sexLab.text = @"";
    }

}



- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
//    self.nikeLabel.text = [change objectForKey:@"new"];
//    UIImage * image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[UserModel sharedUserModel].headImage]]];
//    
//    [self.icon setBackgroundImage:image forState:UIControlStateNormal]; // [UserModel sharedUserModel]
//    
//    self.sexLab.text = [change objectForKey:@"new"];
//    
    [self aa];
    NSLog(@"%@",change);
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    [self setUpFootView];
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:20.0f];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的消息";
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"我的动态";
    } else if (indexPath.row == 2) {
        cell.textLabel.text = @"我的收藏";
    }else{
        cell.textLabel.text = @"我的标签";
    }
   return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
    
    
    if(indexPath.row == 0){

       [UIAlertView showAlertView:@"程序猿正在努力疯狂熬夜加班中添加此板块" andMessage:@"敬请期待" withDelegate:
        nil];
        
        
        
    }else if(indexPath.row == 1){
        
        MyDynamicViewController * myDy = [[MyDynamicViewController alloc]init];
        
        if (self.didBlock) {
            self.didBlock(myDy);
        }
        
    }else if(indexPath.row == 2){
        
        myCollectViewController * myCollect = [[myCollectViewController alloc]init];
        
        if (self.didBlock) {
            self.didBlock(myCollect);
        }
        
    }else if(indexPath.row == 3){
        
        [UIAlertView showAlertView:@"程序猿正在努力疯狂熬夜加班中添加此板块" andMessage:@"敬请期待"];
      
        
        }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 180;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableview.bounds.size.width, 180)];
    //设置头像
    CGFloat imageW = 50;
    CGFloat imageH = 50;
    CGFloat imageX = 20;
    CGFloat imageY = 64;
   _icon = [[UIButton alloc]initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
    
    _icon.backgroundColor = [UIColor whiteColor];
    [_icon addTarget:self action:@selector(imgBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //讲图片设置成圆形
    _icon.layer.cornerRadius = 25;
    _icon.clipsToBounds = YES;
    
    
    [self.view addSubview:_icon];
//    self.icon = imageBTN;
//    
    //设置昵称
    CGFloat nameX = imageX+imageW+10;
    CGFloat nameY = imageY;
    CGFloat nameW = 60;
    CGFloat nameH = imageH/2;
    self.nikeLabel = [[UILabel alloc]initWithFrame:CGRectMake(nameX, nameY, nameW, nameH)];
    [self.nikeLabel setTextColor:[UIColor whiteColor]];
    [self.view addSubview:self.nikeLabel];
    
    
    
    //性别
//    CGFloat sexX = imageX+imageW+10+nameW+10;
//    CGFloat sexY = imageY;
//    CGFloat sexW = 60;
//    CGFloat sexH = imageH/2;
//    UILabel * sexLabel= [[UILabel alloc]initWithFrame:CGRectMake(sexX, sexY, sexW, sexH)];
//    sexLabel.text = @"性别";
//    [sexLabel setTextColor:[UIColor whiteColor]];
//    [self.view addSubview:sexLabel];
    
    //资料完成度
    CGFloat dataX = imageX+imageW+10;
    CGFloat dataY = imageY+nameH;
    CGFloat dataW = 120;
    CGFloat dataH = nameH;
    
    self.sexLab = [[UILabel alloc]initWithFrame:CGRectMake(dataX, dataY, dataW, dataH)];
    self.sexLab .text = @"性别";
    [self.sexLab  setTextColor:[UIColor whiteColor]];
    [self.view addSubview:self.sexLab ];
    
    view.backgroundColor = [UIColor clearColor];
    return view;
}


-(void)setUpFootView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight - 100, self.tableview.size.width, 75)];

    
    CGFloat buttonH = 30;
    CGFloat buttonX = 20;
    CGFloat buttonY = 75-buttonH-10;
    CGFloat buttonW = 40;
   
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(buttonX, buttonY, buttonW+60, buttonH)];
    [button setImage:[UIImage imageNamed:@"setting"] forState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [button setTitle:@"设置" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
   
    [button addTarget:self action:@selector(settingAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    
   [ self.view addSubview:view];
}

-(void)settingAction:(UIButton *)sender
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    YXSetttingController *vc = [[YXSetttingController alloc] init];
    [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
    if (self.didBlock) {
        self.didBlock(vc);
    }
    
}
- (loginView *)loginV{
    if (!_loginV) {
        loginView * v = [[loginView alloc] initWithFrame:CGRectZero];
        
        _loginV = v;
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:_loginV];
    return _loginV;
}

- (void)showLoginView{
    loginView * v = self.loginV;
    CGRect org = v.bounds;
    v.frame = CGRectMake(0, - KScreenHeight, KScreenWidth, KScreenHeight);
    [UIView animateWithDuration:0.5 animations:^{
        v.frame = org;
    }];
    [v orgSubViews];
}




-(void)imgBtnAction:(UIButton *)sender{
    if([UserModel sharedUserModel].isLogin){
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        personalDataViewController *vc = [[personalDataViewController alloc] init];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        if (self.didBlock) {
            self.didBlock(vc);
        }
    
    }else{
        [self showLoginView];
        
    }
}




@end
